/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package jdbcdaos;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import javax.naming.NamingException;
import javax.sql.DataSource;
import jdbcpojos.Producer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

/**
 *
 * @author piotrek
 */
@Repository
public class ProducerDao {

	private JdbcTemplate jdbcTemplate;
	
	public ProducerDao(){
		
	}

	@Autowired
	public void setDataSource(DataSource dataSource) {

		this.jdbcTemplate = new JdbcTemplate(dataSource);

	}

	/**
	 * Dodawanie nowego producenta przez Jdbc
	 * @param Producent producent
	 * @return boolean
	 */
	public boolean addProducer( Producer p ){

		this.jdbcTemplate.update("INSERT INTO producer( name, description ) VALUES( ?, ? )", new Object[] { p.getName(), p.getDescription() });
		return true;
		
	}

	/**
	 * Aktualizacja danych producenta
	 * @param Producer producer
	 * @return boolean
	 */
	public boolean updateProducer( Producer p ){

		this.jdbcTemplate.update("UPDATE producer SET name = ? WHERE id_producer = ?", new Object[] { p.getName(), p.getIdProducer() });
		return true;

	}


	/**
	 * Kasowanie pojedynczego rekordu
	 * @param integer id - identyfikator obiektu producenta
	 * @return boolean
	 */
	public boolean deleteProducer( int id ){

		this.jdbcTemplate.update("DELETE FROM producer WHERE id_producer = ?", new Object[]{ id });

		return true;

	}

	/**
	 * Pobieranie listy producentow przez JDBC
	 * @return List lista producentów
	 */
	public List<Producer> getProducerList(){

		List<Producer> producersList = this.jdbcTemplate.query(
					"SELECT p.id_producer, p.name,p.description FROM producer p WHERE rownum<301",
					new RowMapper<Producer>() {

							public Producer mapRow(ResultSet rs, int rowNum) throws SQLException {
								
								Producer producer = new Producer();

								producer.setIdProducer( rs.getInt("id_producer") );
								producer.setName( rs.getString("name") );
								producer.setDescription( rs.getString("description") );

								return producer;

							}

						}

					);

		return producersList;

	}


	/**
	 * Pobieranie pojedynczego producenta
	 * @param integer id identyfikator producenta
	 * @return Producer obiekt producenta
	 * @throws SQLException
	 * @throws NamingException
	 */
	public Producer getProducer( int id ) throws SQLException, NamingException{

		Producer producer;

		producer = this.jdbcTemplate.queryForObject(
					"SELECT p.name, p.description FROM producer p WHERE p.id_producer = ?",
					new Object[]{ id },
					new RowMapper<Producer>() {

							public Producer mapRow(ResultSet rs, int rowNum)
									throws SQLException {
								Producer p = new Producer();
								p.setName(rs.getString("name"));
								p.setDescription(rs.getString("description"));
								return p;
							}

						}

					);


		return producer;

	}


}